//
//  SwiftUIView.swift
//  STORYZJ_App
//
//  Created by zzzzzzzzzz on 2025/9/29.
//

import SwiftUI
import UIKit
import Translation

struct WEEKMO_TranslateSwiftUIView: View {
    @Binding var showTranslation: Bool
    @Binding var originalText: String
    
    init(showTranslation: Binding<Bool> = .constant(true), originalText: Binding<String> = .constant("")) {
        self._showTranslation = showTranslation
        self._originalText = originalText
    }

    var body: some View {
        if #available(iOS 17.4, *) {
            HStack {
                Text(originalText)
                
                Button() {
                    showTranslation = true
                }label: {
                    Label("Translate", systemImage: "translate")
                        .labelStyle(.iconOnly)
                }
            }
            .padding()
            .translationPresentation(isPresented: $showTranslation, text: originalText) { translatedText in
                print(translatedText)
                originalText = translatedText
            }
        } else {
            // Fallback on earlier versions
        }
    }
}
// 创建UIViewController包装器，以便在Objective-C中调用
@objc class WEEKMO_SwiftUIFatherView: UIView {
    private var textToTranslate: String = ""
    private var showTranslation: Bool = false
    private var hostingController: UIHostingController<WEEKMO_TranslateSwiftUIView>?
    
    @objc override init( frame: CGRect) {
        super.init(frame: frame)
        self.isHidden = true
        updateSwiftUIView()
    }
    
    private func updateSwiftUIView() {
        // 移除旧的hosting controller
        hostingController?.view.removeFromSuperview()
        hostingController?.removeFromParent()
        
        // 创建新的SwiftUI视图
        let swiftUIView = WEEKMO_TranslateSwiftUIView(
            showTranslation: Binding(
                get: { self.showTranslation },
                set: { self.showTranslation = $0 }
            ),
            originalText: Binding(
                get: { self.textToTranslate },
                set: { self.textToTranslate = $0 }
            )
        )
        
        hostingController = UIHostingController(rootView: swiftUIView)
        guard let hostingController = hostingController else { return }
        
        hostingController.view.isHidden = true
        self.addSubview(hostingController.view)
        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            hostingController.view.topAnchor.constraint(equalTo: self.topAnchor),
            hostingController.view.bottomAnchor.constraint(equalTo: self.bottomAnchor),
            hostingController.view.leadingAnchor.constraint(equalTo: self.leadingAnchor),
            hostingController.view.trailingAnchor.constraint(equalTo: self.trailingAnchor)
        ])
    }
    
    @objc func showTranslationView() {
        showTranslation = true
        self.isHidden = false
        hostingController?.view.isHidden = false
    }
    
    @objc func hideTranslationView() {
        showTranslation = false
        self.isHidden = true
        hostingController?.view.isHidden = true
    }
    
    @objc func uploadTranslateStr(str:String){
        self.textToTranslate = str
        // 强制重新创建SwiftUI视图以更新文本
        updateSwiftUIView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



